/**
 * @program: LeetCode
 * @description: LeetCode : 714. 买卖股票的最佳时机含手续费
 * @author: WXY
 * @create: 2023-01-24 16:26
 * @Version 1.0
 **/
public class Num714_maxProfit {
    public static int maxProfit(int[] prices, int fee) {
        int ans = 0;
        int minPrice = prices[0];
        for (int i = 1; i < prices.length; i++) {
            minPrice = Math.min(minPrice, prices[i]);
            if (prices[i] >= minPrice && prices[i] <=  minPrice + fee) {
                continue;
            }
            if (prices[i] > minPrice + fee) {
                ans += prices[i] - minPrice - fee;
                minPrice = prices[i] - fee;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 8, 2, 11};
        int i = maxProfit(arr, 3);
        System.out.println(i);
    }
}
